package com.leyou.test1.constants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author : LiuDong
 * @date : Created in 2018/11/23 16:49
 */
public class DateTurnUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(DateTurnUtils.class);

    /**
     * 本类提供
     *     01. Date, Calendar, Timestamp, String的转化
     *     02. 获取UTC时间方法
     * 参考地址: http://yunnick.iteye.com/blog/1074495
     */
    public static final String PATTERN_SIM = "yyyy-MM-dd";
    public static final String PATTERN_NOR = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_OTH = "yyyy/MM/dd HH:mm:ss";
    public static final String PATTERN_MIL = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String PATTERN_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    /* ++++++++++++++++++++++++++++ step.one: String与Date互转 ++++++++++++++++++++++++++++ */

    /**
     * 1.1 String -> Date
     * @param dateStr 日期字符串
     * @param pattern 格式字符串
     * @return Date
     */
    public static Date turnStringToDate(String dateStr, String pattern) {
        // 忽略对dateStr及pattern的校验
        Date date = null;
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            date = dateFormat.parse(dateStr);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
            LOGGER.info("String -> Date 转换异常: " + e.getMessage());
        }
        return date;
    }

    /**
     * 1.2 Date -> String
     * @param date    日期Date
     * @param pattern 格式字符串
     * @return String
     */
    public static String turnDateToString(Date date, String pattern) {
        // 忽略对date及pattern的校验
        String dateStr = null;
       if (date != null) {
           DateFormat dateFormat = new SimpleDateFormat(pattern);
           dateStr = dateFormat.format(date);
       }
        return dateStr;
    }

    /* ++++++++++++++++++++++++++++ step.two: String与Timestamp互转 ++++++++++++++++++++++++++++ */

    /**
     * 2.1 String -> Timestamp
     * @param dateStr 日期字符串
     * @return Timestamp
     * 注意:
     *     01. dateStr必须符合格式: 'yyyy-MM-dd HH:mm:ss[.S..]', 否则报错
     *     02. 如果dateStr为其他格式, 可以考虑重组字符串, 再进行解析
     */
    public static Timestamp turnStringToTimestamp(String dateStr) {
        Timestamp timestamp = null;
        try {
            timestamp = Timestamp.valueOf(dateStr);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
            LOGGER.info("String -> Timestamp 转换异常: " + e.getMessage());
        }
        return timestamp;
    }

    /**
     * 2.2 Timestamp -> String
     * @param timestamp 时间戳
     * @param pattern   格式字符串
     * @return String
     *     与 1.2 Date -> String 雷同
     */
    public static String turnTimestampToString(Timestamp timestamp, String pattern) {
//        return turnDateToString(timestamp, pattern);
        // 忽略对timestamp及pattern的校验
        String dateStr = null;
        if (timestamp != null) {
            DateFormat dateFormat = new SimpleDateFormat(pattern);
            dateStr = dateFormat.format(timestamp);
        }
        return dateStr;
    }

    /* ++++++++++++++++++++++++++++ step.thr: Date与Timestamp互转 ++++++++++++++++++++++++++++ */

    /**
     * 3.1 Timestamp -> Date
     * @param timestamp 时间戳
     * @return Date
     *     Timestamp extends java.util.Date
     */
    public static Date turnTimestampToDate(Timestamp timestamp) {
        return timestamp;
    }

    /**
     * 3.2 Date -> Timestamp
     * @param date 日期Date
     * @return Timestamp
     */
    public static Timestamp turnDateToTimestamp(Date date) {
        Timestamp timestamp = null;
        if (date != null) {
            timestamp = new Timestamp(date.getTime());
        }
        return timestamp;
    }

    /* ++++++++++++++++++++++++++++ step.for: Date与Calendar互转 ++++++++++++++++++++++++++++ */

    /**
     * 4.1 Calendar -> Date
     * @param calendar 日期calendar
     * @return Date
     */
    public static Date turnCalendarToDate(Calendar calendar) {
        Date date = null;
        if (calendar != null) {
            date = new Date(calendar.getTimeInMillis());
        }
        return date;
    }

    /**
     * 4.2 Date -> Calendar
     * @param date 日期Date
     * @return Calendar
     */
    public static Calendar turnDateToCalendar(Date date) {
        Calendar calendar = null;
        if (date != null) {
            calendar = Calendar.getInstance();
            calendar.setTime(date);
//            calendar.setTimeInMillis(date.getTime());
        }
        return calendar;
    }

    /**
     * 4.3 Calendar -> String
     * @param calendar 日期calendar
     * @param pattern  格式字符串
     * @return String
     */
    public static String turnCalendarToString(Calendar calendar, String pattern) {
        // 忽略对date及pattern的校验
        String dateStr = null;
        if (calendar != null) {
            DateFormat dateFormat = new SimpleDateFormat(pattern);
            dateStr = dateFormat.format(calendar.getTimeInMillis());
        }
        return dateStr;
    }
}
